import binarysearchtree.BinarySearchTree;
import hash.HashBuck;
import hash.HashBuck2;

import java.util.*;

/**
 * 自定义类型 最好去重写 equals 和 hashCode(toString最好也要重写)
 */
class Student{
    private String id;

    public Student(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
public class Test {
    public static void main(String[] args) {
        HashBuck2 buck = new HashBuck2<>();
        Student s1 = new Student("61012345");
        Student s2 = new Student("61012345");
        buck.put(s1,10);
        System.out.println(buck.getValue(s2));
        String s = "123";
        char[] cs = s.toCharArray();
        System.out.println(cs);
        

    }
    public static void main6(String[] args) {
        Student s1 = new Student("61012345");
        Student s2 = new Student("61012345");
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());

        //重写hashCode方法可以通过s2拿到s1
        Map<Student,Integer> map = new HashMap<>();
        map.put(s1,2);
        System.out.println(map.get(s2));

    }

    public static void main5(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("一",1);
        map.put("二",2);
        map.put("三",3);
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry: entries) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
        for (Map.Entry<String, Integer> entry: map.entrySet()) {
            System.out.println("key: "+entry.getKey()+", val:  "+entry.getValue());
        }
        // map不支持迭代器遍历

        System.out.println(map);

        //不会报错
        HashMap<Student,Integer> sMap = new HashMap<>();
        sMap.put(null,0);
        sMap.put(new Student("888"),1);
        sMap.put(new Student("666"),2);

        //set不能存储相同的key 天然去重 底层是一个HashMap 每次存储元素，默认的value是一个Object对象
        HashSet<String> set = new HashSet<>();
        set.add("hello");
        set.add("abcd");
        set.add("hello");
        System.out.println(set);


    }
    public static void main4(String[] args) {
        HashBuck buck = new HashBuck();
        buck.put(1,11);
        buck.put(2,22);
        buck.put(9,99);
        System.out.println(buck.get(9));
    }
    public static void main3(String[] args) {
        Set<String> set = new TreeSet<>();//底层用 map 实现
        set.add("haha");
        set.add("sorry");
        System.out.println(set);
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    public static void main2(String[] args) {
        Map<String,Integer> treeMap = new TreeMap<>();//复杂度: O(logN)
        Map<String,Integer> hashMap = new HashMap<>();//复杂度: O(1) 哈希表:数组+链表+红黑树

        treeMap.put("sunny",3);
        treeMap.put("da",5);
        treeMap.put("hello",2);
        //get() 方法通过 key 获取 value
        //getOrDefault 若对应 key 不存在 返回指定的默认值
        Integer val = treeMap.get("da");
        System.out.println(val);
        //keySet() 返回所有不重复的 key 集合
        Set<String> set = treeMap.keySet();
        System.out.println(set);
        //entrySet() 返回k-v的映射关系
        Set<Map.Entry<String, Integer>> entries = treeMap.entrySet();
        for (Map.Entry<String, Integer> entry: entries) {
            System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
        }
        System.out.println(entries);


    }
    public static void main1(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();
        int[] arr = {5,12,3,2,11,15};
        for (int i = 0; i < arr.length; i++) {
            tree.insert(arr[i]);
        }
        tree.remove(12);

        System.out.println(tree.search(11));

    }
}
